//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Samples.Workflow.UI;

namespace Microsoft.Samples.Workflow.UI.Design
{
    [ActivityDesignerTheme(typeof(InteractionActivityDesignerTheme))]
    internal class InteractionActivityDesigner : SequentialActivityDesigner
    {
        public InteractionActivityDesigner()
            : base()
        {
            this.Expanded = false;
        }

        protected override void PreFilterProperties(IDictionary properties)
        {
            base.PreFilterProperties(properties);

            InteractionActivity interactionActivity = this.Activity as InteractionActivity;
            if (interactionActivity == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_UnexpectedObjectType, "Activity", typeof(InteractionActivity).FullName));
            }

            if (!interactionActivity.ReceiveInput)
            {
                ArrayList keys = new ArrayList(properties.Keys);
                foreach (object key in keys)
                {
                    PropertyDescriptor propDesc = properties[key] as PropertyDescriptor;
                    if (propDesc.Name == "ActivityInput" || propDesc.Name == "ActivityInputType")
                    {
                        properties.Remove(key);
                    }
                }
            }
        }

        protected override void OnActivityChanged(ActivityChangedEventArgs e)
        {
            base.OnActivityChanged(e);

            if (e.Member != null && e.Member.Name == "ReceiveInput")
            {
                TypeDescriptor.Refresh(e.Activity);
            }
        }

        public override bool CanInsertActivities(HitTestInfo insertLocation, ReadOnlyCollection<Activity> activitiesToInsert)
        {
            if (activitiesToInsert == null)
            {
                throw new ArgumentNullException("activitiesToInsert");
            }

            foreach (Activity activity in activitiesToInsert)
            {
                if (activity is InteractionBaseActivity || activity is Navigator ||
                    Helpers.IsOOBStateMachineActivity(activity.GetType()) ||
                    (((InteractionActivity)this.Activity).ReceiveInput && Helpers.IsOOBEventTypeActivity(activity.GetType())))
                {
                    return false;
                }
            }

            return base.CanInsertActivities(insertLocation, activitiesToInsert);
        }

        class InteractionActivityDesignerTheme : CompositeDesignerTheme
        {
            internal const float WatermarkTransparency = 0.25f;
            internal const int ArcDiameter = 8;

            public InteractionActivityDesignerTheme(WorkflowTheme theme)
                : base(theme)
            {
                this.ShowDropShadow = false;
                this.ConnectorStartCap = LineAnchor.DiamondAnchor;
                this.ConnectorEndCap = LineAnchor.ArrowAnchor;
                this.ForeColor = Color.FromArgb(0xFF, 0x10, 0x10, 0x10);
                this.BorderColor = Color.FromArgb(0xFF, 0x49, 0x77, 0xb4);
                this.BorderStyle = DashStyle.Solid;
                this.BackColorStart = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xDF);
                this.BackColorEnd = Color.FromArgb(0xFF, 0xA9, 0xA3, 0xFF);
            }
        }
    }
}
